define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'printmanager/printing/index' + location.search,
                    add_url: 'printmanager/printing/add',
                    edit_url: 'printmanager/printing/edit',
                    setorg_url: 'printmanager/printing/setorg',
                    setdesigner: 'printmanager/printing/setdesigner',
                    del_url: 'printmanager/printing/del',
                    done_url: 'printmanager/printing/done',
                    multi_url: 'printmanager/printing/multi',
                    check_url: 'printmanager/printing/check',
                    detail_url: 'printmanager/printing/detail',
                    table: 'self_printing',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [{
                            checkbox: true
                        },
                        {
                            field: 'id',
                            title: __('Id')
                        },
                        {
                            field: 'user_id',
                            title: __('User_id'),
                            searchable: false,
                            formatter: function(v,r,i){
                                return r.user_info.nickname
                            }
                        },
                        {
                            field: 'designer_id',
                            title: __('设计师'),
                            searchable: false,
                            formatter: function(v,r,i){
                                if(!r.designer_info) return ''
                                html = '<div style="text-align:left">';
                                html += '<a>设计师：'+r.designer_info.real_name+'</a><br>'
                                html += '<a>状态：'+r.design_product_info.status_text+'</a>'
                                html += '</div>';
                                return html
                            }
                        },
                        {
                            field: 'org_id',
                            title: __('承印商'),
                            searchable: false,
                            formatter: function(v,r,i){
                                if(!r.org_info) return ''
                                html = '<div style="text-align:left">';
                                html += '<a>承印单位：'+r.org_info.org_name+'</a><br>'
                                html += '<a>承印状态：'+r.product_info.status_text+'</a>'
                                html += '</div>';
                                return html
                            }
                        },
                        {
                            field: 'title',
                            title: __('Title'),
                            searchable: false,
                        },
                        {
                            field: 'category_id',
                            title: __('类型'),
                            searchable: false,
                            formatter: function(v,r,i){
                                return r.category ? r.category.name: '未知'
                            }
                        },
                        {
                            field: 'used_time',
                            title: __('Used_time'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'telephone',
                            title: __('联系方式'),
                            searchable: false,
                            formatter: function(v,r,i){
                                var html = '';
                                if(r.linkname){
                                    html += '<p>联系人：'+r.linkname+'</p>'
                                }
                                if(r.telephone){
                                    html += '<p>电话：'+r.telephone+'</p>'
                                }
                                if(r.address){
                                    html += '<p>地区：'+r.address+'</p>'
                                }
                                return html
                            }
                        },
                        {
                            field: 'status',
                            title: __('Status'),
                            searchList: {
                                "0": __('待验证'),
                                "1": __('验证有效待下单'),
                                "2": __('无效需求'),
                                "3": __('已付款下单'),
                                "4": __('已完成'),
                            },
                            formatter: Table.api.formatter.status
                        },
                        {
                            field: 'createtime',
                            title: __('Createtime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.buttons,
                            buttons: [
                                {
                                    name: 'detail',
                                    title: __('详情'),
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    icon: 'fa fa-navicon',
                                    url: $.fn.bootstrapTable.defaults.extend.detail_url,
                                    success: function (data, ret) {
                                        table.bootstrapTable('refresh', {});
                                        Layer.alert(ret.msg);
                                    },
                                    visible: function (row) {
                                        return true
                                    }
                                },
                                {
                                    name: 'refuse',
                                    title: __('无效需求'),
                                    classname: 'btn btn-xs btn-primary btn-prompt',
                                    extend: ' data-prompt="2" data-datakey="reason" data-table-id="table" placeholder="请输入原因"',
                                    icon: 'fa fa-times',
                                    url: $.fn.bootstrapTable.defaults.extend.check_url + "?action=refuse",
                                    success: function (data, ret) {
                                        table.bootstrapTable('refresh', {});
                                        Layer.alert(ret.msg);
                                    },
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        return row.status == 0;
                                    }
                                },
                                {
                                    name: 'check',
                                    title: __('需求有效'),
                                    classname: 'btn btn-xs btn-success btn-ajax',
                                    icon: 'fa fa-check',
                                    url: $.fn.bootstrapTable.defaults.extend.check_url  + "?action=check",
                                    success: function (data, ret) {
                                        table.bootstrapTable('refresh', {});
                                        Layer.alert(ret.msg);
                                    },
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        return row.status == 0;
                                    }
                                },
                                
                                {
                                    name: 'paydone',
                                    title: __('用户已付款'),
                                    confirm: "请确认是否已经付款",
                                    classname: 'btn btn-xs btn-primary btn-ajax',
                                    icon: 'fa fa-check',
                                    url: $.fn.bootstrapTable.defaults.extend.check_url + "?action=paydone",
                                    success: function (data, ret) {
                                        table.bootstrapTable('refresh', {});
                                        Layer.alert(ret.msg);
                                    },
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        return row.status == 1;
                                    }
                                },
                                {
                                    name: 'dispatch',
                                    title: __('分配印刷机构'),
                                    classname: 'btn btn-xs btn-success btn-dialog',
                                    icon: 'fa fa-cog',
                                    url: $.fn.bootstrapTable.defaults.extend.setorg_url,
                                    success: function (data, ret) {
                                        table.bootstrapTable('refresh', {});
                                        Layer.alert(ret.msg);
                                    },
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        return row.status == 3 && row.org_id == 0;
                                    }
                                },
                                {
                                    name: 'designer',
                                    title: __('分配设计师'),
                                    classname: 'btn btn-xs btn-warning btn-dialog',
                                    icon: 'fa fa-cog',
                                    url: $.fn.bootstrapTable.defaults.extend.setdesigner,
                                    success: function (data, ret) {
                                        table.bootstrapTable('refresh', {});
                                        Layer.alert(ret.msg);
                                    },
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        return row.status == 3 && row.designer_id == 0;
                                    }
                                },

                                {
                                    name: 'check',
                                    title: __('已完成'),
                                    classname: 'btn btn-xs btn-success btn-ajax',
                                    icon: 'fa fa-check',
                                    url: $.fn.bootstrapTable.defaults.extend.done_url,
                                    success: function (data, ret) {
                                        table.bootstrapTable('refresh', {});
                                        Layer.alert(ret.msg);
                                    },
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        return row.status == 3;
                                    }
                                },
                            ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        setorg: function () {
            Controller.api.bindevent();
        },
        setdesigner: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});